﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using QRCodeBluetooth.BUS;
using QRCodeBluetooth.DAL;
using QRCodeBluetooth.Models;
using System;
using System.Collections.Generic;

namespace QRCodeBluetooth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RequestController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public RequestController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
        )
        {
            _jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }

        /// <summary>
        /// 同步凭证
        /// </summary>
        [HttpPost]
        [Route("UpdateAudit")]
        public QRCodeVoucherInfoReturn UpdateAudit(QRCodeVoucherInfo qRCodeVoucherInfo)
        {
            LogHelper.Log(JsonConvert.SerializeObject(qRCodeVoucherInfo));
            QRCodeVoucherInfoReturn message = new QRCodeVoucherInfoReturn();
            List<QRCodeVoucherInfoReturnData> qRCodeVoucherInfoReturnDatas = new List<QRCodeVoucherInfoReturnData>();
            message.method = "UpdateAudit";
            try
            {
                AppDAL appDAL = new AppDAL();
                if (qRCodeVoucherInfo.method == "UpdateAudit")
                {
                    int projectid = appDAL.QueryProject(qRCodeVoucherInfo.ProjectKey);
                    if (projectid > 0)
                    {
                        foreach (var item in qRCodeVoucherInfo.data)
                        {
                            QRCodeVoucherInfoReturnData rCodeVoucherInfoReturnData = new QRCodeVoucherInfoReturnData();
                            rCodeVoucherInfoReturnData.auditid = item.auditid;
                            rCodeVoucherInfoReturnData.deviceName = item.deviceName;
                            rCodeVoucherInfoReturnData.deviceNo = item.deviceNo;
                            rCodeVoucherInfoReturnData.project_key = qRCodeVoucherInfo.ProjectKey;
                            bool delete_result = appDAL.DeleteVoucher(item.deviceNo, item.auditid);
                            string sql = string.Empty;
                            foreach (var value in item.floor)
                            {
                                sql += string.Format("INSERT INTO voucher_record(startTime,endTime,deviceName,deviceNo,auditId,dfloor,pfloor,qrcodeData) VALUES ('{0}', '{1}', '{2}', '{3}', {4}, '{5}', '{6}', '{7}');", Convert.ToDateTime(value.startTime).ToString("yyyy-MM-dd HH:mm:ss"), Convert.ToDateTime(value.endTime).ToString("yyyy-MM-dd HH:mm:ss"), item.deviceName, item.deviceNo, item.auditid, value.dfloor, value.pfloor, value.qrcodeData);
                            }
                            LogHelper.Log(sql);
                            bool insert_result = appDAL.InsertVoucher(sql);
                            if (insert_result)
                            {
                                rCodeVoucherInfoReturnData.result = 0;
                            }
                            else
                            {
                                rCodeVoucherInfoReturnData.result = 1;
                            }
                            qRCodeVoucherInfoReturnDatas.Add(rCodeVoucherInfoReturnData);
                        }
                        message.data = qRCodeVoucherInfoReturnDatas;
                    }
                    else
                    {
                        message.code = 1;
                        message.msg = "找不到项目信息";
                    }
                }
                else
                {
                    message.code = 1;
                    message.msg = "同步凭证失败";
                }
            }
            catch (Exception ex)
            {
                message.code = 1;
                message.msg = "同步凭证异常";
            }

            return message;
        }

        /// <summary>
        /// 删除乘梯凭证
        /// </summary>
        [HttpPost]
        [Route("DeleteVoucher")]
        public QRCodeVoucherInfoReturn DeleteVoucher(QRCodeVoucherInfo qRCodeVoucherInfo)
        {
            QRCodeVoucherInfoReturn message = new QRCodeVoucherInfoReturn();
            List<QRCodeVoucherInfoReturnData> qRCodeVoucherInfoReturnDatas = new List<QRCodeVoucherInfoReturnData>();
            message.method = "DeleteVoucher";
            try
            {
                AppDAL appDAL = new AppDAL();
                if (qRCodeVoucherInfo.method == "DeleteVoucher")
                {
                    int projectid = appDAL.QueryProject(qRCodeVoucherInfo.ProjectKey);
                    if (projectid > 0)
                    {
                        foreach (var item in qRCodeVoucherInfo.data)
                        {
                            QRCodeVoucherInfoReturnData rCodeVoucherInfoReturnData = new QRCodeVoucherInfoReturnData();
                            rCodeVoucherInfoReturnData.auditid = item.auditid;
                            rCodeVoucherInfoReturnData.deviceName = item.deviceName;
                            rCodeVoucherInfoReturnData.deviceNo = item.deviceNo;
                            rCodeVoucherInfoReturnData.project_key = qRCodeVoucherInfo.ProjectKey;
                            bool delete_result = appDAL.DeleteVoucher(item.deviceNo, item.auditid);
                            if (delete_result)
                            {
                                rCodeVoucherInfoReturnData.result = 0;
                            }
                            else
                            {
                                rCodeVoucherInfoReturnData.result = 1;
                            }
                            qRCodeVoucherInfoReturnDatas.Add(rCodeVoucherInfoReturnData);
                        }
                        message.data = qRCodeVoucherInfoReturnDatas;
                    }
                    else
                    {
                        message.code = 1;
                        message.msg = "找不到项目信息";
                    }
                }
                else
                {
                    message.code = 1;
                    message.msg = "删除凭证失败";
                }
            }
            catch (Exception ex)
            {
                message.code = 1;
                message.msg = "删除凭证异常";
            }


            return message;
        }

        /// <summary>
        /// 修改申请审核状态
        /// </summary>
        [HttpPost]
        [Route("UpdateAuditState")]
        public QRCodeAuditReturn UpdateAuditState(QRCodeAudit qRCodeAudit)
        {
            QRCodeAuditReturn message = new QRCodeAuditReturn();
            List<QRCodeAuditReturnData> list = new List<QRCodeAuditReturnData>();
            message.method = "UpdateAuditState";
            try
            {
                AppDAL appDAL = new AppDAL();
                if (qRCodeAudit.method == "UpdateAuditState")
                {
                    int projectid = appDAL.QueryProject(qRCodeAudit.ProjectKey);
                    if (projectid > 0)
                    {
                        foreach (var item in qRCodeAudit.data)
                        {
                            QRCodeAuditReturnData rCodeAuditReturnData = new QRCodeAuditReturnData();
                            rCodeAuditReturnData.AuditID = item.AuditID;
                            bool insert_result = appDAL.UpdateAudit(item.AuditID, item.AuditStatus, projectid);
                            if (insert_result)
                            {
                                rCodeAuditReturnData.result = 0;
                            }
                            else
                            {
                                rCodeAuditReturnData.result = 1;
                            }
                            list.Add(rCodeAuditReturnData);
                        }
                        message.data = list;
                    }
                    else
                    {
                        message.code = 1;
                        message.msg = "找不到项目信息";
                    }
                }
                else
                {
                    message.code = 1;
                    message.msg = "同步审核记录失败";
                }
            }
            catch (Exception ex)
            {
                message.code = 1;
                message.msg = "同步审核记录异常";
            }

            return message;
        }

        /// <summary>
        /// 删除审核记录
        /// </summary>
        [HttpPost]
        [Route("DeleteAuditState")]
        public QRCodeAuditReturn DeleteAuditState(QRCodeAudit qRCodeAudit)
        {
            LogHelper.Log(JsonConvert.SerializeObject(qRCodeAudit));
            QRCodeAuditReturn message = new QRCodeAuditReturn();
            List<QRCodeAuditReturnData> list = new List<QRCodeAuditReturnData>();
            message.method = "DeleteAuditState";
            try
            {
                AppDAL appDAL = new AppDAL();
                if (qRCodeAudit.method == "DeleteAuditState")
                {
                    int projectid = appDAL.QueryProject(qRCodeAudit.ProjectKey);
                    if (projectid > 0)
                    {
                        foreach (var item in qRCodeAudit.data)
                        {
                            QRCodeAuditReturnData rCodeAuditReturnData = new QRCodeAuditReturnData();

                            bool insert_result = appDAL.DeleteAudit(item.AuditID, projectid);
                            if (insert_result)
                            {
                                rCodeAuditReturnData.result = 0;
                            }
                            else
                            {
                                rCodeAuditReturnData.result = 1;
                            }
                            rCodeAuditReturnData.AuditID = item.AuditID;
                            list.Add(rCodeAuditReturnData);
                        }
                        message.data = list;
                    }
                    else
                    {
                        message.code = 1;
                        message.msg = "找不到项目信息";
                    }
                }
                else
                {
                    message.code = 1;
                    message.msg = "删除审核记录失败";
                }
            }
            catch (Exception ex)
            {
                message.code = 1;
                message.msg = "删除审核记录异常";
            }

            return message;
        }

        /// <summary>
        /// 查询未同步的记录
        /// </summary>
        [HttpPost]
        [Route("QueryUnSyncAudit")]
        public UnSyncAuditReturn QueryUnSyncAudit(UnSyncAudit unSyncAudit)
        {
            LogHelper.Log(JsonConvert.SerializeObject(unSyncAudit));
            UnSyncAuditReturn message = new UnSyncAuditReturn();

            message.method = "QueryUnSyncAudit";
            try
            {
                AppDAL appDAL = new AppDAL();
                if (unSyncAudit.method == "QueryUnSyncAudit")
                {
                    int projectid = appDAL.QueryProject(unSyncAudit.ProjectKey);
                    if (projectid > 0)
                    {
                        List<AppAuditRecord> list = appDAL.QuerySyncAuditRecord(projectid);
                        message.data = list;
                    }
                    else
                    {
                        message.code = 1;
                        message.msg = "找不到项目信息";
                    }
                }
                else
                {
                    message.code = 1;
                    message.msg = "同步审核记录失败";
                }
            }
            catch (Exception ex)
            {
                message.code = 1;
                message.msg = "同步审核记录异常";
            }

            return message;
        }

        /// <summary>
        /// 更新同步的记录
        /// </summary>
        [HttpPost]
        [Route("UpdateSyncAuditFlag")]
        public string UpdateSyncAuditFlag(UpdateUnSyncAuditFlag updateUnSyncAuditFlag)
        {
            LogHelper.Log(JsonConvert.SerializeObject(updateUnSyncAuditFlag));

            try
            {
                AppDAL appDAL = new AppDAL();
                if (updateUnSyncAuditFlag.method == "UpdateSyncAuditFlag")
                {
                    int projectid = appDAL.QueryProject(updateUnSyncAuditFlag.ProjectKey);
                    if (projectid > 0)
                    {
                        bool re = appDAL.UpdateSyncFlag(updateUnSyncAuditFlag.ids);
                    }
                    else
                    {
                        LogHelper.Log("找不到项目信息");
                    }
                }
                else
                {
                    LogHelper.Log("更新同步审核记录失败");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log("更新同步审核记录异常");
            }

            return "";
        }
    }
}
